import path from 'path'
import type { UserConfig, ConfigEnv } from 'vite'
import { loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import eslintPlugin from 'vite-plugin-eslint'
import vueJsx from '@vitejs/plugin-vue-jsx'
import monacoEditorPlugin from 'vite-plugin-monaco-editor'

// https://vitejs.dev/config/
export default ({ command, mode }: ConfigEnv): UserConfig => {
  const root = process.cwd()
  const env: any = loadEnv(mode, root)
  const { VITE_PORT } = env
  return {
    plugins: [vue(), vueJsx(), monacoEditorPlugin(), eslintPlugin()],
    define: {
      'process.env': {} // 将上面的env赋值到这里
    },
    server: {
      host: true,
      port: VITE_PORT
    },
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
        '@yang': path.resolve(__dirname, './src/library')
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@import "@/theme/index.scss";'
        }
      },
      postcss: {
        plugins: [require('tailwindcss'), require('autoprefixer')]
      }
    }
  }
}
